package cn.mavor.day0910.Student;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentDataProcessor {
    private List<Student> students;

    public StudentDataProcessor(List<String> lines) {
        students = new ArrayList<>();
        for (String line : lines) {
            String[] parts = line.split(":|,");
            if (parts.length == 3) {
                students.add(new Student(parts[0], parts[1], Integer.parseInt(parts[2])));
            }
        }
    }

    public void writeNameAndScore() throws IOException {
        List<String> lines = new ArrayList<>();
        for (Student student : students) {
            lines.add(student.getName() + ":" + student.getScore());
        }
        FileUtils.writeAllLines(FileUtils.STUDS_FILE, lines);
    }

    public String findHighestScorer() {
        Student highestScorer = null;
        for (Student student : students) {
            if (highestScorer == null || student.getScore() > highestScorer.getScore()) {
                highestScorer = student;
            }
        }
        return highestScorer != null ? highestScorer.getName() + ":" + highestScorer.getScore() : "无学生信息";
    }

    public void calculateClassAverages() {
        Map<String, List<Integer>> classScores = new HashMap<>();
        for (Student student : students) {
            classScores.computeIfAbsent(student.getClassName(), k -> new ArrayList<>()).add(student.getScore());
        }

        for (Map.Entry<String, List<Integer>> entry : classScores.entrySet()) {
            String className = entry.getKey();
            List<Integer> scores = entry.getValue();
            double average = scores.stream().mapToInt(Integer::intValue).average().orElse(0.0);
            System.out.printf("%s:%.2f%n", className, average);
        }
    }

    public void queryScore(String name) {
        for (Student student : students) {
            if (student.getName().equals(name)) {
                System.out.println("系名: " + student.getClassName() + ", 成绩: " + student.getScore());
                return;
            }
        }
        System.out.println("未找到该学生的信息。");
    }

    public void updateScore(String name, int newScore) throws IOException {
        boolean found = false;
        for (Student student : students) {
            if (student.getName().equals(name)) {
                student.setScore(newScore);
                found = true;
            }
        }
        if (found) {
            writeStudentsToFile();
            System.out.println("成绩已更新。");
        } else {
            System.out.println("未找到该学生的信息。");
        }
    }

    public void writeStudentsToFile() throws IOException {
        List<String> lines = new ArrayList<>();
        for (Student student : students) {
            lines.add(student.toString());
        }
        FileUtils.writeAllLines(FileUtils.TEMP_FILE, lines);
        FileUtils.deleteFile(FileUtils.DATA_FILE);
        FileUtils.moveFile(FileUtils.TEMP_FILE, FileUtils.DATA_FILE);
    }
}